package com.cwf.quality.common.strategy.factory;

import com.cwf.quality.common.enums.DataAnalyzerEnum;
import com.cwf.quality.common.strategy.analyzer.IDataAnalyzer;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class DataAnalyzerFactory {
    private static Map<String, IDataAnalyzer> dataAnalyzerMap = new HashMap<>();

    static {
        for (DataAnalyzerEnum dataAnalyzerEnum : DataAnalyzerEnum.values()) {
            try {
                Class<?> clazz = Class.forName(dataAnalyzerEnum.getClassName());
                IDataAnalyzer iDataAnalyzer = (IDataAnalyzer) clazz.newInstance();
                dataAnalyzerMap.put(dataAnalyzerEnum.getType(), iDataAnalyzer);
            } catch (ClassNotFoundException e) {
                log.error(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                log.error(e.getMessage(), e);
            } catch (InstantiationException e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    public static IDataAnalyzer getDataAnalyzer(String dataAnalyzerType) {
        return dataAnalyzerMap.get(dataAnalyzerType);
    }
}
